package cn.com.server.common;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import cn.com.server.common.util.DateUtil;
public class MyLocale { 
	private String propertyName = "package";
	private String country;
	private String launage;
	private Locale locale;
	private ResourceBundle resourceBundle;
	
	public MyLocale() {
		setLocale(new Locale("zh", "CN"));
		setResourceBundle(ResourceBundle.getBundle(propertyName, locale));
	}
	public Locale  checkLocale(String launage) {
		if(launage != null) {  
            if (launage.equals("cn")) {  
                // 汉语-中国  
                locale = new Locale("zh", "CN");  
            } else if (launage.equals("en")) {  
                // 英语-美国  
                locale = new Locale("en", "US");  
            } else if (launage.equals("fr")) {  
                // 法国  
                locale = new Locale("fr", "FR");  
            } else if (launage.equals("es")) {
            	 //西班牙
            	locale = new Locale("es", "ES");  
			}
            //以上内容是根据配置文件的配置信息实例化Locale对象。  
		} 
		return locale;
	}
	
	@SuppressWarnings("unused")
	private  Locale getDefaultLocale() {
        String language = "cn";
        return new Locale(language);
    }

	
	public MyLocale(String language) {
		setLocale(checkLocale(language));
		if (null == locale) {
			 locale = new Locale("zh", "CN");
		}
		setResourceBundle(ResourceBundle.getBundle(propertyName, locale));
	}
	
	public String getText(String key) {
        key = convert(key);
		String text = key;
        try {
            text = resourceBundle.getString(key);
        } catch(Exception e) {}
        text = text.replaceAll("\\{time\\}", DateUtil.getCurrentDateTimeAsString());
        return text;
	}
	
	public String getText(String key, String arg) {
        key = convert(key);
		String text = key;
        try {
            text = resourceBundle.getString(key);
        } catch(Exception e) {}
        text = text.replaceAll("\\{time\\}", DateUtil.getCurrentDateTimeAsString());
        if (null != text) {
            if (null == arg) {
                arg = "";
            }
			text = text.replaceAll("\\{" + 0 + "\\}", arg);
		}
		
		return text;
	}

	
	public String getText(String key, List<?> args) {
        key = convert(key);
		String text = key;
        try {
            text = resourceBundle.getString(key);
        } catch(Exception e) {}
		
        text = text.replaceAll("\\{time\\}", DateUtil.getCurrentDateTimeAsString());
		
        if (null != text) {
			for (int i = 0; i < args.size(); i++) {
                String arg = "";
                if (null != arg) {
                    arg = "";
                    if (null != args.get(i)) {
                        arg = args.get(i).toString();
                    }
                }
				text = text.replaceAll("\\{" + i + "\\}", arg);
			}
		}
		
		return text;
	}

	public String getText(String key, String... args) {
        key = convert(key);
		String text = key;
        try {
            text = resourceBundle.getString(key);
        } catch(Exception e) {}
		
        text = text.replaceAll("\\{time\\}", DateUtil.getCurrentDateTimeAsString());
		
        if (null != text) {
			for (int i = 0; i < args.length; i++) {
                String arg = "";
                if (null != arg) {
                    arg = args[i].toString();
                }
				text = text.replaceAll("\\{" + i + "\\}", arg);
			}
		}
		return text;
	}

    public String convert(String key) {
        return key.replaceAll("_", ".").toLowerCase();
    }
	
	public String getCountry() {
		return country;
	}
	
	public void setCountry(String country) {
		this.country = country;
	}
	
	public String getLaunage() {
		return launage;
	}
	
	public void setLaunage(String launage) {
		this.launage = launage;
	}

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	public ResourceBundle getResourceBundle() {
		return resourceBundle;
	}

	public void setResourceBundle(ResourceBundle resourceBundle) {
		this.resourceBundle = resourceBundle;
	}

	public String getPropertyName() {
		return propertyName;
	}

	public void setPropertyName(String propertyName) {
		this.propertyName = propertyName;
	}
	
}
